<?php
/**
 * Created by PhpStorm.
 * User: moon
 * Date: 2019/1/8
 * Time: 11:14
 * 发送 CURL 请求
 */

namespace application\depend;


class CurlRequest
{

    //处理POST请求
    public static function post($postData = [], $uploadFile = false)
    {
        $curl = new AppCurl();
        $result = $curl->curlPost(APP_SERV_ROUTE, $postData, $uploadFile);
        return self::handlerResult($result);
    }

    //处理POST请求  -- 内部网关
    public static function internalPost($postData = [], $uploadFile = false)
    {
        $curl = new AppCurl();
        $result = $curl->curlPost(INTERNAL_GATEWAY_ROUTE, $postData, $uploadFile);
        return self::handlerResult($result);
    }

    private static function handlerResult($result)
    {
        $return = new Appresult();
        if (empty($result)) {
            $return->result = ERR_DB_EXECUTE_ERROR;
            $return->msg = "接口未返回数据！";
        } else {
            if (is_string($result)) {
                $tmp = json_decode($result);
                if (is_object($tmp)) {
                    $tmp = (array)$tmp;
                }
                if (isset($tmp['result'])) {
                    $return = $tmp;
                } else {
                    $return->result = ERR_DB_DATA_EMPTY;
                    $return->msg = "接口返回数据不标准！[38]";
                    $return->rows = $result;
                }

            } else {
                if (!is_array($result)) {
                    $result = (array)$result;
                }
                if (isset($result['result'])) {
                    $return = $result;
                } else {
                    $return->result = ERR_DB_DATA_EMPTY;
                    $return->msg = "接口返回数据不标准！[48]";
                    $return->rows = $result;
                }

            }//end else
        }//end if
        if (is_object($return)) {
            $return = (array)$return;
        }
        return $return;
    }    //end
}